<template>
    <div class="cart-bottom-bar">
        <div class="check-content">
            <check-button class="check-button"
                          :is-checked="isSelectAll"
                          @click.native="checkClick"/>
            <span>全选</span>
        </div>
        <div class="price">
            合计：{{totalPrice}}
        </div>

        <div class="calculate" @click="calcClick">
            去计算({{checkLength}})
        </div>

    </div>
</template>

<script>
    import CheckButton from "../../../components/content/checkButton/CheckButton";
    import {mapGetters} from 'vuex'

    export default {
        name: "CartBottomBar",
        components: {CheckButton},
        computed: {
            ...mapGetters(['cartList']),
            totalPrice() {
                return '￥' + this.$store.state.cartList.filter(item => {
                    return item.checked
                }).reduce((preValue, item) => {
                    return preValue + item.newprice * item.count
                }, 0).toFixed(2)
            },
            checkLength() {
                return this.cartList.filter(item => item.checked).length
            },
            isSelectAll(){
                if (this.cartList.length ===0) return false
                // return  !(this.cartList.filter(item=> !item.checked).length)

                return !this.cartList.find(item => !item.checked)
            }
        },
        methods:{
            checkClick(){
                if (this.isSelectAll){
                    // 全部不选中
                    this.cartList.forEach(item => item.checked = false)
                }else {
                    // 全部选中
                    this.cartList.forEach(item => item.checked = true)
                }
            },
            calcClick(){
                if (!this.cartList.find(item => item.checked===true)){
                    console.log(this.$toast.show);
                    this.$toast.show('请选择购买的商品',1500)

                }
            }
        }
    }
</script>

<style scoped>
    .cart-bottom-bar {
        position: relative;
        display: flex;
        height: 40px;
bottom: 40px;
        line-height: 40px;
        background-color: #eee;

    }

    .check-content {
        display: flex;
        align-items: center;
        margin-left: 10px;
    }

    .check-button {
        width: 20px;
        height: 20px;
        line-height: 20px;
        margin-right: 5px;
    }

    .price {
        margin-left: 20px;
        flex: 1;
        align-items: center;
    }

    .calculate {
        width: 90px;
        background-color: orange;
    }
</style>
